// pages/chooseDetail/chooseDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    price: 0,
    choose: "",
    model: "",
    install: "",
    place: "",
    index: 0,
    id: 0,
    num: 1,
    key: 0,
    factor: 0,
    newData: [],
    choose: [],
    feeList: [],
    list: [],
    totalPrice: 0,
    splice: "",
    model: [{
      name: "枪机",
      check: false
    }, {
      name: "半球",
      check: false
    }, {
      name: "球机",
      check: false
    }],
    install: [{
      name: "室内",
      check: false
    }, {
      name: "室外",
      check: false
    }],
    place: [{
      name: "墙装",
      check: false
    }, {
      name: "立杆",
      check: false
    }],
    place2: [{
      name: "离地超5米",
      check: false
    }, {
      name: "离地5米内",
      check: false
    }],
    parentCateId: 0,

    site: 0
  },
  request: function() {
    var that = this;
    wx.request({
      url: 'https://www.xiaozheng8.com/service/cate/findAll',
      // url: 'http://192.168.43.244:8002/service/cate/findAll',
      method: 'post',
      success: function(res) {
        console.log(res)
        that.setData({
          list: res.data.data.sonList[that.data.id].sonList[that.data.index].sonList[that.data.site].prop2List,
          title: res.data.data.sonList[that.data.id].name + "-" + res.data.data.sonList[that.data.id].sonList[that.data.index].name,
          siteTitle: res.data.data.sonList[that.data.id].sonList[that.data.index].sonList[that.data.site].name
        })
        var choose = [];
        var data = [];
        var names = [];
        var feeList = [];
        var price = 0;
        for (var i = 0; i < that.data.list.length; i++) {
          that.data.list[i].prop2s[0].check = true;
          choose.push(that.data.list[i].prop2s[0])
          data.push(that.data.list[i].prop2s[0].propId);
          names.push(that.data.list[i].prop2s[0].name);
          feeList.push(that.data.list[i].prop2s[0].fee);
          that.setData({
            list: that.data.list,
            choose: choose,
            newData: data,
            names: names,
            feeList: feeList
          })
        }
        for (var i = 0; i < that.data.feeList.length; i++) {
          price = price + that.data.feeList[i];
        }
        console.log(price);
        that.setData({
          originalPrice: price,
          price: Math.round(price * that.data.factor)
        })
        var totalPrice = Math.round(that.data.originalPrice * that.data.factor);
        that.setData({
          totalPrice: totalPrice
        })
        console.log(that.data.choose);
      },
      fail: function(res) {},
      complete: function(res) {},
    })
    // wx.request({
    //   url: 'https://www.xiaozheng8.com/service/factor/findAll',
    //   data: '',
    //   method: 'post',
    //   success: function (res) {
    //     console.log(res);
    //   },
    //   fail: function (res) { },
    //   complete: function (res) { },
    // })
  },
  num: function(e) {
    var that=this;
    var num = this.validateNumber(e.detail.value);
    this.setData({
      num:num
    })
    // if(num===0){
    //   wx.showToast({
    //     title: '数量最低设置为1',
    //   })
    //   num=1;
    // }
    console.log(num);
  
  },
  blur(){
    if(this.data.num==0||this.data.num==""){
      wx.showToast({
        title: '数量最低设置为1',
        icon:"none"
      })
      this.setData({
        num:1
      })
    }
    var that=this;
    var totalPrice = Math.round(that.data.originalPrice * that.data.factor * this.data.num);
    console.log(totalPrice);
    this.setData({
      price: Math.round(this.data.originalPrice * this.data.factor),
      totalPrice: totalPrice
    })
  },
  validateNumber(val) {
    return val.replace(/\D/g, '')
  },
  model: function(e) {
    var check = e.currentTarget.dataset.check;
    var index = e.currentTarget.dataset.index;
    var parentId = e.currentTarget.dataset.parentid;
    var propId = e.currentTarget.dataset.propid;
    console.log(index);
    var that = this;
    console.log(that.data.list);
    var newData = [];
    console.log(parentId + "parentId");
    for (var i = 0; i < this.data.list.length; i++) {
      console.log(that.data.list[i]);
      // for (var j = 0; j < that.data.list[i].prop2s.length; j++) {
      if (index + 1 > that.data.list[i].prop2s.length) {
        continue;
      } else {
        if (that.data.list[i].prop2s[index].parentId == parentId) {

          that.data.list[i].prop2s[index].check = true;
          // console.log(that.data.list[i].prop2s[index].propId)
          console.log("------------------")
          that.setData({
            list: that.data.list,
          })
          newData.push(that.data.list[i].prop2s[index].propId);

          // var choose = [];
          for (var j = 0; j < that.data.list[i].prop2s.length; j++) {
            if (j != index) {
              that.data.list[i].prop2s[j].check = false;
              that.setData({
                list: that.data.list
              })
            }
            if (that.data.list[i].prop2s[j].check == true) {

              // choose.push(that.data.list[i].prop2s[j])
              var choose = that.data.list[i].prop2s[j];
              var data = that.data.list[i].prop2s[j].propId;
              var fee = that.data.list[i].prop2s[j].fee;
              var name = that.data.list[i].prop2s[j].name;
              that.data.choose.splice(i, 1, choose);
              that.data.newData.splice(i, 1, data);
              that.data.feeList.splice(i, 1, fee);
              that.data.names.splice(i, 1, name);
              console.log(that.data.choose);
              that.setData({
                choose: that.data.choose,
                newData: that.data.newData,
                feeList: that.data.feeList
              })
              console.log(that.data.choose);
            }

          };
          console.log(that.data.newData);


        }
      }
    }
    var price = 0;
    for (var i = 0; i < that.data.feeList.length; i++) {
      price = price + that.data.feeList[i];
    }
    console.log(price);

    that.setData({
      originalPrice: price,
      price: Math.round(that.data.originalPrice * that.data.factor)
    });
    var totalPrice = Math.round(that.data.originalPrice * that.data.factor * that.data.num);;
    that.setData({
      totalPrice: totalPrice
    })
    console.log(that.data.originalPrice)
  },
  submit: function() {
    var that = this;
    console.log(this.data.totalPrice+"价格")
    // 将所有数据生成json数据并保存
    var orderData = {};
    var data = [];
    var inTitle = {};
    var titleContent = [];
    console.log(that.data.splice)
    if (that.data.splice != "" && that.data.splice != undefined) {
      console.log(that.data.price * that.data.num);
      console.log(that.data.totalPrice);
      inTitle.props = that.data.newData;
      inTitle.first = that.data.id;
      inTitle.second = that.data.index;
      inTitle.third = that.data.site;
      inTitle.cateId = that.data.parentCateId;
      console.log(that.data.parentCateId);
      inTitle.number = that.data.num;
      inTitle.originalPrice = that.data.originalPrice * that.data.num,
        inTitle.price =that.data.totalPrice;
      inTitle.titleName = that.data.title;
      orderData.title = that.data.title;
      inTitle.names = that.data.names;
      inTitle.siteTitle = that.data.siteTitle;
      titleContent.push(inTitle)
      orderData.titleContent = titleContent;
      var order = that.data.title;
      console.log(order);
      var orderContent = wx.getStorageSync("orderData");
      console.log(orderContent);
      console.log(titleContent[0]);
      console.log(inTitle);
      for (var i = 0; i < orderContent.length; i++) {
        console.log(orderContent[i].title);
        console.log(that.data.title);
        if (orderContent[i].title == that.data.title) {
          orderContent[i].titleContent.splice(that.data.splice, 1, inTitle);
          console.log(orderContent[i]);
          wx.setStorageSync("orderData", orderContent);
        };
       
      }
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1]; //当前页面
      var prevPage = pages[pages.length - 2]; //上一个页面
      console.log(that.data.splice);
      console.log(prevPage.data.orderData);
      prevPage.setData({
        changeDetail: true
      });
      console.log(orderContent)
    
    } else {
      // console.log(wx.getStorageSync("i"));
      // if (wx.getStorageSync("i") == undefined || wx.getStorageSync("i") == "") {
      //   that.setData({
      //     i: 0
      //   })
      // } else {
      //   that.setData({
      //     i: parseInt(wx.getStorageSync("i"))
      //   })
      // }

      console.log(that.data.newData);
      inTitle.props = that.data.newData;
      inTitle.first = that.data.id;
      inTitle.second = that.data.index;
      inTitle.third = that.data.site;
      inTitle.cateId = that.data.parentCateId;
      inTitle.number = that.data.num;
      inTitle.originalPrice = that.data.originalPrice * that.data.num,
        inTitle.price = that.data.totalPrice;
      inTitle.titleName = that.data.title;
      orderData.title = that.data.title;
      inTitle.names = that.data.names;
      inTitle.siteTitle = that.data.siteTitle;
      titleContent.push(inTitle)
      orderData.titleContent = titleContent;



      // orderData.newData = that.data.newData;
      // orderData.first = that.data.id;
      // orderData.second = that.data.index;
      // orderData.third = that.data.site;
      // orderData.number = that.data.num;
      // orderData.price = that.data.price;
      // orderData.title=that.data.title;
      // orderData.names=that.data.names
      // orderData.siteTitle = that.data.siteTitle;
      var orderDataList = [];
      if (wx.getStorageSync("orderDataList") == "" || wx.getStorageSync("orderDataList") == undefined) {
        orderDataList = [];
      } else {
        orderDataList = wx.getStorageSync("orderDataList");
      }


      orderDataList.push(orderData);
      console.log(orderDataList);
      wx.setStorageSync("orderDataList", orderDataList);
    }
    // console.log(that.data.titleSite == 1);
    // console.log(that.data.titleSite);
    if (that.data.titleSite == 1) {
      wx.navigateBack({
        delta: 2
      })
    } else if (that.data.back == 1) {
      // var pages = getCurrentPages();
      // var currPage = pages[pages.length - 1];   //当前页面
      // var prevPage = pages[pages.length - 2];  //上一个页面
      // console.log(that.data.splice);
      // console.log(prevPage.data.orderData);
      // prevPage.setData({
      //   orderData: prevPage.data.orderData.splice(that.data.splice, 1)
      // });

      wx.navigateBack({
        delta: 1
      })

    } else {
      wx.navigateBack({
        delta: 3
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    var that = this;
    this.setData({
      index: options.index,
      site: options.site,
      id: options.id,
      parentCateId: options.cateId,
      titleSite: options.title,
      back: options.back,
      splice:options.splice
    });
    if (wx.getStorageSync("factor") == undefined || wx.getStorageSync("factor") == "") {
      that.setData({
        factor: 1
      })
    } else {
      that.setData({
        factor: wx.getStorageSync("factor")
      })
    }
  },
  // 增加数量，减少数量
  addCount: function(e) {
    var price = this.data.price
    console.log(this);
    var that = this;
    var num = this.data.num;
    num++;
    price = num * price
    this.setData({
      num: num,
      price: Math.round(this.data.originalPrice * this.data.factor)
    });
    var totalPrice = Math.round(that.data.originalPrice * that.data.factor * that.data.num);;
    that.setData({
      totalPrice: totalPrice
    })
    console.log(this.data.totalPrice);
  },
  minusCount: function(e) {
    var num = this.data.num;
    var that = this;
    if (num > 1) {
      num--;
    } else {
      num = 1
    }
    this.setData({
      num: num,
      price: Math.round(this.data.originalPrice * this.data.factor)
    });
    var totalPrice = Math.round(that.data.originalPrice * that.data.factor * that.data.num);
    that.setData({
      totalPrice: totalPrice
    })
    console.log(this.data.price);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.request();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})